package com.ruoyi.wms.service;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.wms.domain.bo.stock.StoreInformationBo;
import com.ruoyi.wms.domain.entity.stock.StoreInformation;
import com.ruoyi.wms.domain.vo.NameAndIdList;
import com.ruoyi.wms.domain.vo.StoreInformationVo;
import com.ruoyi.wms.mapper.StoreInformationMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 门店信息Service业务层处理
 *
 * @author 邓滔滔
 * @date 2024-11-19
 */
@RequiredArgsConstructor
@Service
public class StoreInformationService {

    private final StoreInformationMapper storeInformationMapper;

    /**
     * 查询门店信息
     */
    public StoreInformationVo queryById(Long id){
        return storeInformationMapper.selectVoById(id);
    }

    /**
     * 查询门店信息列表
     */
    public TableDataInfo<StoreInformationVo> queryPageList(StoreInformationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StoreInformation> lqw = buildQueryWrapper(bo);
        Page<StoreInformationVo> result = storeInformationMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询门店信息列表
     */
    public List<StoreInformationVo> queryList(StoreInformationBo bo) {
        LambdaQueryWrapper<StoreInformation> lqw = buildQueryWrapper(bo);
        return storeInformationMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StoreInformation> buildQueryWrapper(StoreInformationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StoreInformation> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getStoreName()), StoreInformation::getStoreName, bo.getStoreName());
        lqw.eq(bo.getStoreStatus() != null, StoreInformation::getStoreStatus, bo.getStoreStatus());
        lqw.like(StringUtils.isNotBlank(bo.getManagerName()), StoreInformation::getManagerName, bo.getManagerName());
        return lqw;
    }

    /**
     * 新增门店信息
     */
    public void insertByBo(StoreInformationBo bo) {
        StoreInformation add = MapstructUtils.convert(bo, StoreInformation.class);
        add.setId(RandomUtil.randomLong(11));
        storeInformationMapper.insert(add);
    }

    /**
     * 修改门店信息
     */
    public void updateByBo(StoreInformationBo bo) {
        StoreInformation update = MapstructUtils.convert(bo, StoreInformation.class);
        storeInformationMapper.updateById(update);
    }

    /**
     * 批量删除门店信息
     */
    public void deleteByIds(Collection<Long> ids) {
        storeInformationMapper.deleteBatchIds(ids);
    }

    /**
     * 查询门店名称和id
     * @return
     */
    public List<NameAndIdList> list() {
        List<NameAndIdList> list = new ArrayList<>();
        for (StoreInformation storeInformation : storeInformationMapper.selectList()) {
            NameAndIdList nameAndIdList = new NameAndIdList();
            nameAndIdList.setId(storeInformation.getId());
            nameAndIdList.setName(storeInformation.getStoreName());
            list.add(nameAndIdList);
        }
        return list;
    }
}
